/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.util.pilas;

import model.util.listas.SimpleList;

/**
 *
 * @author Gustavo Canul
 */
public class StackList implements Stack {

    private SimpleList simpleList;
    private int cantidad;

    public StackList() {
        this.simpleList = new SimpleList();
        this.cantidad = 0;
    }

    @Override
    public int size() {
        return cantidad;
    }

    @Override
    public boolean isEmpty() {
        return simpleList.isEmpty();
    }

    @Override
    public void push(Object o) {
        simpleList.insertAtFront(o);
        cantidad++;
    }

    @Override
    public Object pop() throws StackEmptyException {
        try {
            Object objeto = simpleList.removeFromFront();
            cantidad--;
            return objeto;
        } catch (Exception e) {
            throw new StackEmptyException();
        }
    }

    @Override
    public Object top() throws StackEmptyException {
        if (isEmpty()) {
            throw new StackEmptyException();
        } else {
            return simpleList.getFirstNode().getData();
        }
    }
}
